"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : describe sql
Case Name   : desc/describe查询执行计划，format参数验证，format=format_name格式，取值不带引号
Create At   : 2023-01-12
@zou_jialiang0501162244
Description :
    1.兼容B库建表并插入数据
    2.desc/describe查询执行计划，format取值text
    3.desc/describe查询执行计划，format取值json
    4.desc/describe查询执行计划，format取值xml
    5.desc/describe查询执行计划，format取值yaml
    6.清理环境
Expect      :
    1.成功
    2.输出text格式计划
    3.输出json格式计划
    4.输出xml格式计划
    5.输出yaml格式计划
    6.成功
History     :
    modified by @zou_jialiang050,2023.04.13，指定SQL语句执行库为兼容B库
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Describe(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_describe_0046'
        self.db_name = 'db_describe_0046'
        self.log.info('----创建兼容B库----')
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility='B';"
        res_createdb = self.pri_sh.execut_db_sql(create_db)
        self.log.info(res_createdb)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res_createdb,
                      f"创建兼容B库失败")

    def test_describe_table(self):
        text1 = '-----step1:建表并插入数据;expect:成功-----'
        self.log.info(text1)
        create_tb = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(
        c_int int primary key, c_varchar varchar(20), c_text text);
        insert into {self.tb_name} values
        (1, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk')), 
        (2, 'azAZ09—字符', lpad('abc', '50', 'a@123汉字&^%djgk'));'''
        res_createtb = self.pri_sh.execut_db_sql(
            sql=create_tb, dbname=f"{self.db_name}")
        self.log.info(res_createtb)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res_createtb,
                      f'建表失败:{text1}')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res_createtb,
                      f'插入数据失败:{text1}')

        text2 = '-----step2:desc/describe查询执行计划，format取值text;' \
                'expect:输出text格式计划-----'
        self.log.info(text2)
        sql_text = f'''desc format = text select * from {self.tb_name} 
        where c_int > 2 order by 1;
        describe format = TEXT select * from {self.tb_name} 
        where c_int > 2 order by 1;'''
        res_text = self.pri_sh.execut_db_sql(
            sql=sql_text, dbname=f"{self.db_name}")
        self.log.info(res_text)
        self.assertEqual(res_text.count('Bitmap Index Scan on'), 2,
                         f'执行失败:{text2}')
        self.assertEqual(res_text.count('Sort Key'), 2, f'执行失败:{text2}')

        text3 = '-----step3:desc/describe查询执行计划，format取值json;' \
                'expect:输出json格式计划-----'
        self.log.info(text3)
        sql_json = f'''set explain_perf_mode = normal;
        desc format = json select * from {self.tb_name} 
        where c_int between 1 and 100;
        describe format = Json select * from {self.tb_name} 
        where c_int between 1 and 100;'''
        res_json = self.pri_sh.execut_db_sql(
            sql=sql_json, dbname=f"{self.db_name}")
        self.log.info(res_json)
        self.assertEqual(res_json.count('"Node Type": "Bitmap Heap Scan",'),
                         2, f'执行失败:{text3}')

        text4 = '-----step4:desc/describe查询执行计划，format取值xml;' \
                'expect:输出xml格式计划-----'
        self.log.info(text4)
        sql_xml = f'''set explain_perf_mode = normal;
        describe format = XML select * from {self.tb_name} where c_int=1;
        desc format = xml select * from {self.tb_name} where c_int=1;'''
        res_xml = self.pri_sh.execut_db_sql(
            sql=sql_xml, dbname=f"{self.db_name}")
        self.log.info(res_xml)
        self.assertEqual(res_xml.count('<Node-Type>Index Scan</Node-Type>'),
                         2, f'执行失败:{text4}')

        text5 = '-----step5:desc/describe查询执行计划，format取值yaml;' \
                'expect:输出xml格式计划-----'
        self.log.info(text5)
        sql_yaml = f'''set explain_perf_mode = normal;
        describe format = yaml delete from {self.tb_name} where c_int>1;
        desc format = YAML delete from {self.tb_name} where c_int>1;'''
        res_yaml = self.pri_sh.execut_db_sql(
            sql=sql_yaml, dbname=f"{self.db_name}")
        self.log.info(res_yaml)
        self.assertEqual(res_yaml.count('Operation: "Delete"'), 2,
                         f'执行失败:{text5}')

    def tearDown(self):
        text6 = '-----step6:清理环境;expect:成功-----'
        self.log.info(text6)
        drop_db = self.pri_sh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_db)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_db,
                      f'执行失败:{text6}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
